var express = require('express');
var router = express.Router();
const db = require("./db");
var multer = require('multer');
//导入连个内置模块
const path = require('path')
const fs = require('fs')
var objMulter = multer({ dest: './public/images' });
router.use(objMulter.any());
/* GET home page. */


router.get('/', function (req, res, next) {
  res.render('index', { title: '我是你爹' });
});

router.post('/infor', function (req, res, next) {
  req.files.forEach(item => {
    let oldPath = item.path;
    let newPath = "./public/images/" + item.originalname;
    fs.renameSync(oldPath, newPath);
    let sql = `INSERT INTO xiaoshuo (id, picter) VALUES (NULL, '${newPath}');`
    db(sql, (err, data) => {
      if (err) {
        throw err;
      }
      res.send({
        code: 1,
        msg: '成功',
      })
    })
  });

})


// 首页图片接口
router.get("/dimg", (req, res) => {
  var sql = `select * from yinyue`;
  db(sql, (err, data) => {
    res.send(data)
  })
})

// 小说图片接口
router.get("/ximg", (req, res) => {
  var sql = `select * from xiaoshuo`;
  db(sql, (err, data) => {
    res.send(data)
  })
})


// 小说搜索接口
router.get("/xiaoimg", (req, res) => {
  if (Object.keys(req.query).length > 0) {
    var { novel} = req.query;
    console.log(req.query);
  } else {
    var { novel} = req.body;
    console.log(req.body);
  }
  let sql = `select * from xiaoshuo where novel like '%${novel}%'`;
  db(sql, (err, data) => {
    if(err){
      throw err
  }
    res.send({
      data:data
    })
  })
})



// 注册或者登录查询
router.all('/dengSearch', function (req, res, next) {
  if (Object.keys(req.query).length > 0) {
    var { phone} = req.query;
    console.log(req.query);
  } else {
    var { phone} = req.body;
    console.log(req.body);
  }
  let sql = `select * from zhuce where phone = '${phone}'`;
  db(sql, (err, data) => {
    if (err) {
      throw err
    }
    res.send({
      code:1,
      msg:"注册成功",
      data:data
    })
  })
});

// 登录
router.all('/denglu', function (req, res, next) {
  if (Object.keys(req.query).length > 0) {
    var { phone,password} = req.query;
  } else {
    var { phone,password} = req.body;
  }
  let sql = `select * from zhuce where phone = '${phone}' and password = '${password}'`;
  db(sql, (err, data) => {
    if (err) {
      throw err
    }
    res.send({
      code:1,
      msg:"登录成功",
      data:data
    })
  })
});

// 首页音乐查询全部
router.all('/musicSearchAll', function (req, res, next) {
  let sql = `select * from music where 1`;
  db(sql, (err, data) => {
    if (err) {
      throw err
    }
    res.send({
      data: data
    })
  })
});
// 首页彩铃
router.all('/cailingSearchAll', function (req, res, next) {
  let sql = `select * from cailing where 1`;
  db(sql, (err, data) => {
    if (err) {
      throw err
    }
    res.send({
      data: data
    })
  })
});
// 注册接口
router.post('/zhuceSearchAll', function (req, res, next) {
  let {phone,password} = req.body;
  let sql = `INSERT INTO zhuce (phone,password) VALUES ('${phone}', '${password}')`;
  db(sql,(err,data)=>{
    if(err){
      throw err
    }
    res.send({
      code:1,
      msg:"注册成功",
      data:data
    })
  })
});
// 登录接口
router.all('/passwordSearchAll', function (req, res, next) {
  let sql = `select * from zhuce where 1`;
  db(sql, (err, data) => {
    if (err) {
      throw err
    }
    res.send({
      data: data
    })
  })
});
module.exports = router;
